﻿import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

import '../util/util.dart';

//Line
class Line extends StatelessWidget {
  final double height;
  final Color color;

  const Line({this.height: 1.0, this.color}) : assert(height >= 0.0);

  @override
  Widget build(BuildContext context) {
    Color lineColor = null == color ? Colors.grey : color;
    return Container(
      height: height,
      color: lineColor,
    );
  }
}

class ClickLayout extends StatefulWidget {
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  final Widget child;
  final VoidCallback onPressed;
  final ValueChanged<bool> onHighlightChanged;
  final GestureLongPressCallback onLongPress;
  final Color background;
  final Color highlightBackground;
  final Decoration decoration;
  final double width;
  final double height;
  final AlignmentGeometry alignment;
  final BoxConstraints constraints;

  const ClickLayout(
      {this.width,
      this.height,
      this.child,
      this.onPressed,
      this.padding,
      this.margin,
      this.onHighlightChanged,
      this.onLongPress,
      this.background,
      this.alignment,
      this.highlightBackground,
      this.decoration,
      this.constraints});

  @override
  State<StatefulWidget> createState() {
    return _ClickLayoutState();
  }
}

class _ClickLayoutState extends State<ClickLayout> {
  Color background;
  bool pressed = false;

  void _update() {
    setState(() {
      background = pressed ? widget.highlightBackground : widget.background;
    });
  }

  @override
  void initState() {
    super.initState();
    _update();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        constraints: widget.constraints,
        width: widget.width,
        height: widget.height,
        color: background,
        padding: widget.padding,
        margin: widget.margin,
        alignment: widget.alignment,
        decoration: widget.decoration,
        child: InkWell(
            onTap: widget.onPressed,
            onLongPress: widget.onLongPress,
            onHighlightChanged: null == widget.onHighlightChanged
                ? _handleHighlightChanged
                : widget.onHighlightChanged,
            highlightColor: widget.highlightBackground,
            child: widget.child));
  }

  void _handleHighlightChanged(value) {
    pressed = value;
    _update();
  }
}

class ScaffoldView extends StatelessWidget {
  final String title;
  final Widget body;
  final Widget drawer;
  final Widget endDrawer;
  final VoidCallback onBack;
  final bool showLeading;
  final bool centerTitle;
  final List<Widget> appBarActions;
  final Widget floatingActionButton;
  final Color bodyColor;
  final Color appBarColor;
  final bool initAppbar;

  ScaffoldView({
    Key key,
    this.title,
    this.drawer,
    this.endDrawer,
    this.onBack,
    this.bodyColor,
    this.appBarColor,
    this.showLeading = true,
    this.centerTitle = false,
    this.body,
    this.appBarActions,
    this.floatingActionButton,
    this.initAppbar = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool show = null == showLeading ? true : showLeading;
    Color bodyColor =
        null == this.bodyColor ? Color(0XFFEEEFF3) : this.bodyColor;
    Color appBarColor =
        null == this.appBarColor ? Color(0xFF3F51B5) : this.appBarColor;

    Widget leading = show
        ? IconButton(icon: Icon(Icons.arrow_back), onPressed: onBack)
        : null;

    Widget appbar = initAppbar
        ? AppBar(
            actions: appBarActions,
            centerTitle: centerTitle,
            backgroundColor: appBarColor,
            leading: leading,
            title: Text(TextUtils.replace(title, ''),
                style: TextStyle(color: Colors.white)))
        : null;

    return Scaffold(
      key: key,
      body: body,
      drawer: drawer,
      endDrawer: endDrawer,
      backgroundColor: bodyColor,
      floatingActionButton: floatingActionButton,
      floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
      appBar: appbar,
    );
  }
}

class MultipleStatusLayout extends StatefulWidget {
  final MultipleStatus status;

  const MultipleStatusLayout({this.status});

  @override
  State<StatefulWidget> createState() {
    return _MultipleStatusState();
  }
}

class _MultipleStatusState extends State<MultipleStatusLayout> {
  @override
  Widget build(BuildContext context) {
    return ScaffoldView(title: '');
  }

  void changeStatus(MultipleStatus status) {}
}

enum MultipleStatus {
  LOADING, //加载
  EMPTY, // 没有数据
  ERROR, // 网络错误
  HIDE, // 隐藏
  NORMAL, // 正常页面
}

enum Visibility {
  visible,
  invisible,
  offscreen,
  gone,
}

class ViewSub extends StatelessWidget {
  final Visibility visibility;
  final Widget child;
  final Widget removeChild;

  ViewSub({
    this.child,
    @required this.visibility,
  }) : this.removeChild = Container();

  @override
  Widget build(BuildContext context) {
    if (visibility == Visibility.visible) {
      return child;
    } else if (visibility == Visibility.invisible) {
      return new IgnorePointer(
          ignoring: true, child: Opacity(opacity: 0.0, child: child));
    } else if (visibility == Visibility.offscreen) {
      return new Offstage(offstage: true, child: child);
    } else {
      return removeChild;
    }
  }
}


class CyxmSpinWidget<T> extends StatelessWidget {
  final Key key;
  final List<DropdownMenuItem<T>> items;

  final T value;
  final ValueChanged<T> onChanged;

  CyxmSpinWidget(
      {this.key, @required this.items, this.value, @required this.onChanged})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8),
      alignment: Alignment.centerRight,
      decoration: BoxDecoration(
          border: Border.all(color: Color(0xFF0099CC), width: 1),
          borderRadius: BorderRadius.all(Radius.circular(5))),
      child: DropdownButtonHideUnderline(
        child: DropdownButton(
            items: items,
            value: value,
            onChanged: onChanged,
            isDense: true,
            style: TextStyle(fontSize: 16.0, color: Colors.black)),
      ),
    );
  }
}

class PhotoWallpaper extends StatelessWidget {
  final Key key;
  final String title;
  final BuildContext context;
  final Widget child;
  final VoidCallback back;
  final bool fullScreen;
  final Alignment alignment;
  final List<Widget> appBarActions;

  PhotoWallpaper(
    this.context, {
    this.key,
    this.title,
    this.child,
    this.fullScreen = false,
    this.back,
    this.alignment = Alignment.center,
    this.appBarActions,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget bodyWidget =
        fullScreen ? Container(child: child, alignment: alignment) : child;

    return Stack(
      children: <Widget>[
        _wallpaper(),
        ScaffoldView(
            bodyColor: Colors.transparent,
            appBarColor: Colors.transparent,
            initAppbar: !fullScreen,
            title: TextUtils.replace(title, ''),
            appBarActions: appBarActions,
            onBack: null == back ? _back : back,
            body: SafeArea(
              child: bodyWidget,
            ))
      ],
    );
  }

  Widget _wallpaper() {
    return Image(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      image: AssetImage('images/splash_background.jpg'),
      fit: BoxFit.fill,
    );
  }

  _back() {
    Navigator.pop(context);
  }
}
